package com.zdar.processing;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.XmlReader;
import com.badlogic.gdx.utils.XmlReader.Element;
import com.zdar.entities.UnitTemplate;
import com.zdar.processing.XmlHandler;

/**
 * Class which reads the XML file containing all playable unit types
 * Eventually will need some abstraction to read other XML files, but we'll worry about that when we need them.
 * To use this class, first create a new XmlReader object.
 * Then decide whether you want the ArrayList {getUnitList()} or HashMap {getUnitMap()}
 * @author danbye50
 *
 */

public class XmlHandler extends DefaultHandler{
	
	private ArrayList<UnitTemplate> unitList = new ArrayList<UnitTemplate>();
	private HashMap<String, UnitTemplate> unitMap = new HashMap<String, UnitTemplate>();
	
	private void readUnitXml(){
		XmlReader reader = new XmlReader();
		FileHandle file = Gdx.files.internal("data/units.xml");
		Element root;
		try {
			root = reader.parse(file);
			//Array<Element> units = root.getChildrenByName("unit");
			Iterator it_unit = root.getChildrenByName("unit").iterator();
			
			while (it_unit.hasNext()){
				// so now we have to iterate through each units children?
				Element el_unit = (Element) it_unit.next();
				
				// I dunno just do a bunch of printouts here to figure it out
				System.out.println(el_unit.getText());
				System.out.println(el_unit.get("type"));
				
				// If this is correct then get the unit's children
				// Send string to another method which determines what it's for
				// that then sends a call to another method which adjusts the UnitTemplate's info
			}
			
			/*for (Element child : units)
			{
				// I don't like this way because then everything has to be in the right order all the time
				// Instead we should iterate through here and check for the conditions or whatever
				// Yeah
				
				// Create a new UnitTemplate and set its info with the xml info
				// put into UnitList
				
				String unitType = root.get("type");
			    //String description = child.getChildByName("description").getAttribute("text");
				String description = child.getChildByName("description").getText();	//maybe this will work too.. lot's a testing
			    String wildcard = child.getChildByName("wildcard").getAttribute("slot");
			    String display_name = child.getChildByName("display_name").getAttribute("damage");
			    // do what you wish with these
			}*/
			
		} catch (IOException e) {
			e.printStackTrace();
		}		
		
	}
	
	/*public void readXml(int dude){
		xml = new XmlReader();
		//String filename = "assets/data/units.xml";
		filehandle = Gdx.files.internal("data/units.xml"); //hopefully this doesn't try to go into the android folder. if not try files.local
		try {
			xml_element = xml.parse(filehandle);	
		} catch (IOException e) {e.printStackTrace();}
		
		iterator_unit = xml_element.getChildrenByName("unit").iterator();
		while(iterator_unit.hasNext()){
			XmlReader.Element unit_element = (XmlReader.Element)iterator_unit.next();
		}
	}*/
	
	
	
	
	/*/ OLD!
	private UnitTemplate unit;	
	private String temp;

	
	public ArrayList<UnitTemplate> getUnitList(){
		if (unitList.size() == 0){
			try {
				readXML();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (unitList.size() == 0){
			System.out.println("Error: Unit list XML is empty.");
		}
		return unitList;
	}
	
	public HashMap<String, UnitTemplate> getUnitMap(){
		if (unitMap.size() == 0){
			try {
				readXML();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (unitMap.size() == 0){
			System.out.println("Error: Unit list XML is empty.");
		}
		return unitMap;
	}
	
	public void readXML() throws ParserConfigurationException, SAXException, IOException{

		//Create a "parser factory" for creating SAX parsers
        SAXParserFactory spfac = SAXParserFactory.newInstance();

        //Now use the parser factory to create a SAXParser object
        SAXParser sp = spfac.newSAXParser();
        
        //Create an instance of this class; it defines all the handler methods
        XmlHandler xmlHandler = new XmlHandler();

        //Finally, tell the parser to parse the input and notify the handler
        sp.parse("assets/data/testing.xml", xmlHandler);       
        
        xmlHandler.readList();
        unitList = xmlHandler.unitList;	//Because we're creating an instance of the class within an instance of the class
        unitMap = xmlHandler.unitMap;
	}
	
	//just read things out.
	private void readList(){
		System.out.println("Current number of units: " + unitList.size());
		for (UnitTemplate i: unitList){
			System.out.println(i.toString());
		}
	}
	
	//When parser encounters plain text, this method is called
	public void characters(char[] buffer, int start, int length){
		temp = new String(buffer, start, length);
	}
	
	//Every time the parser encounters the beginning of a new element, it calls this method
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		temp = "";
		if (qName.equalsIgnoreCase("UNIT")){
			unit = new UnitTemplate();
			unit.setType(attributes.getValue("type"));
		}
	}
	
	//when the end of an element is ecountered, this method is called
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (qName.equalsIgnoreCase("Unit")){
			//add to list
			unitList.add(unit);
			unitMap.put(unit.getType(), unit);
			
		} else if (qName.equalsIgnoreCase("display_name")){
			unit.setDisplayName(temp);
		} else if (qName.equalsIgnoreCase("wildcard")){
			unit.setWildcard(temp);
		} else if (qName.equalsIgnoreCase("description")){
			unit.setDescription(temp);
		}		
	}
	*/
}
